/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.retail;

import com.opensymphony.xwork2.ActionSupport;
import database.DBFactory;
import database.ProductDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Category;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Thai Ha Cong
 */
public class DataAction extends ActionSupport {

    private String pid;
    private String pde;
    private String textHere;
    private String button;

    public DataAction() {
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getPde() {
        return pde;
    }

    public void setPde(String pde) {
        this.pde = pde;
    }

    public String getTextHere() {
        return textHere;
    }

    public void setTextHere(String textHere) {
        this.textHere = textHere;
    }

    public String getButton() {
        return button;
    }

    public void setButton(String button) {
        this.button = button;
    }

    @Action(value = "data", results = {
        @Result(name = "success", location = "/invalid.jsp")
    })
    @Override
    public String execute() throws Exception {
        this.setTextHere(this.getTextHere() + this.getButton());
        System.out.print(this.getTextHere());

//        Connection con = DBFactory.createConnection("test");
//        String sqlst = "INSERT INTO textarea(content) VALUES('" + this.getTextHere() + "')";
//        PreparedStatement ps = con.prepareStatement(sqlst);
//        ps.executeUpdate();
//        this.setTextHere(textHere.replaceAll("\r\n", "<br>"));
//        System.out.println("----------\n\n" + button);
        return SUCCESS;
    }

    @Action(value = "description", results = {
        @Result(name = "success", location = "/include/draft.jsp"),
        @Result(name = "error", location = "/include/draft.jsp")
    })
    public String des() {
//        System.out.println("aaaaaaaaaaaaaaa");
        try {
            Connection con = DBFactory.createConnection("onlineshopping");
            String sqlst = "UPDATE product SET product.details = ? "
                    + "WHERE product.idProduct = ?";
            PreparedStatement ps = con.prepareStatement(sqlst);
            ps.setString(1, pde);
            ps.setString(2, pid);
            ps.executeUpdate();

            addActionMessage("Done");
            return SUCCESS;
        } catch (SQLException ex) {
            Logger.getLogger(DataAction.class.getName()).log(Level.SEVERE, null, ex);
            addActionError("Fail");
        }

        return ERROR;
    }

    public static void main(String[] args) {
        try {
            String s = "";
            Connection con = DBFactory.createConnection("test");
            String sqlst = "SELECT content FROM textarea WHERE idtextarea='1'";
            PreparedStatement ps = con.prepareStatement(sqlst);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                s = (rs.getString(1));
            }

            if (s.contains("\r\n")) {
                System.out.println("111");
            }

            String s2 = s.replaceAll("\r\n", "<br>");
            System.out.println(s2);

            int quantity = Integer.parseInt("0");
            System.out.println(quantity);


            System.out.println("----------------------------");

            ProductDAO productDAO = new ProductDAO();

            List<Category> cat = productDAO.getAllCategories();
            for (Category c : cat) {
                System.out.println(c.toString());
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
//        ColumnPositionMappingStrategy cpms = new ColumnPositionMappingStrategy();
//        cpms.setType(BidItem.class);
//        String[] columns = new String[] {"date", "keyword", "qualityScore", "cpc", "impressions", "clicks", "ctr", "avgCpc", "cost", "avgPosition"};
//        cpms.setColumnMapping(columns);
//        
//        CsvToBean ctb = new CsvToBean();
//        bidItems = ctb.parse(cpms, reader);
//        String line = "";
//        BufferedReader br = new BufferedReader(new FileReader(inputFile));
//        while ((line = br.readLine()) != null) {
//
//            // use comma as separator
//            String[] country = line.split(",");
//
//            System.out.println("Country [code= " + country[4]
//                    + " , name=" + country[5] + "]");
//
//        }
}